import 'package:flutter/material.dart';

class RegisterPage1 extends StatefulWidget {
  const RegisterPage1({Key? key}) : super(key: key);

  @override
  _RegisterPageState1 createState() => _RegisterPageState1();
}
class Message {
  final String text;
  final bool isUser; // 用于识别消息是来自用户还是AI

  Message({required this.text, required this.isUser});
}

class ChatBubble extends StatelessWidget {
  final Message message;

  ChatBubble({required this.message});

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: message.isUser ? Alignment.centerRight : Alignment.centerLeft,
      child: Container(
        padding: EdgeInsets.all(10),
        margin: EdgeInsets.all(10),
        decoration: BoxDecoration(
          color: message.isUser ? Colors.blue : Colors.grey[300],
          borderRadius: BorderRadius.circular(12),
        ),
        child: Text(
          message.text,
          style: TextStyle(
            color: message.isUser ? Colors.white : Colors.black,
          ),
        ),
      ),
    );
  }
}
class _RegisterPageState1 extends State<RegisterPage1> {
  bool showPassword = false;
  List<Message> messages = []; // 存储聊天消息的列表

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("AI小助手"),
      ),
      body: Column(
        children: [
          Expanded(
            child: ListView.builder(
              itemCount: messages.length,
              itemBuilder: (BuildContext context, int index) {
                return ChatBubble(message: messages[index]);
              },
            ),
          ),
          _buildMessageComposer(),
        ],
      ),
    );
  }

  Widget _buildMessageComposer() {
    return Container(
      padding: EdgeInsets.all(8),
      color: Colors.grey[200],
      child: Row(
        children: [
          Expanded(
            child: TextField(
              decoration: InputDecoration(
                hintText: "输入消息...",
                border: OutlineInputBorder(),
              ),
              onSubmitted: (value) {
                _sendMessage(value, true); // 将用户消息添加到聊天中
              },
            ),
          ),
          IconButton(
            icon: Icon(Icons.send),
            onPressed: () {
              _sendMessage("AI的回复在这里！", false); // 模拟AI的回复
            },
          ),
        ],
      ),
    );
  }

  void _sendMessage(String text, bool isUserMessage) {
    setState(() {
      messages.add(Message(text: text, isUser: isUserMessage));
    });
  }
}